var queryData = {};


var nowSelectDept;
var isAll;
jQuery(function() {	
	//查询按钮
	initQueryParam();
	jQuery("#queryBtn").on('click',function(){
		queryAllChart();
	})
	//还原按钮
	jQuery("#datehy").on('click',function(){
		var mydate = new Date();
		var begindate = mydate.getFullYear()+"-01-01"
		var enddate = "" + mydate.getFullYear() + "-";
		enddate += ((mydate.getMonth()+1)<10 ? '0'+(mydate.getMonth()+1) : (mydate.getMonth()+1)) + "-";
		enddate += (mydate.getDate()<10 ? '0'+mydate.getDate() : mydate.getDate());
		jQuery("#beginDate").val(begindate);
		$('#endDate').val(getDay(1));
		
		
		queryAllChart();
	})
    
	myThenChart = echarts.init(document.getElementById('thenDiv'));//趋势图
	myDeptChart = echarts.init(document.getElementById('myDeptChart'));//部门分析
	myAreaChart = echarts.init(document.getElementById('myAreaChart'));//片区分析
	yonChart = echarts.init(document.getElementById('yonChart'));//状态分析
	countChart = echarts.init(document.getElementById('countChart'));//预计订货量
	//趋势分析
	myThenChart.setOption({
        title : {
            text: '趋势分析'
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:['信息数量'],
            itemWidth:40,
            itemHeight:20,
            textStyle:{
                fontSize:15,
                color:'#fff'
            }
        },
        toolbox: {
            show : true,
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : []
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLabel : {
                    formatter: '{value}'
                }
            }
        ],
        series : [
            {
                name:'所有信息数量',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            },{
                name:'成功信息数量',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            },
        ]
    });
	
	//单击趋势图表打开
	myThenChart.on('click',function(params){
    	openContractList(1,params.name);
    });
	//部分分析
    myDeptChart.setOption({
        title: {
            text: '部门分析'
        },
        tooltip: {},
        legend: {
            data:['机会数量(个)']
        },
        xAxis: {
            data: []
        },
        yAxis: {
        	 
        },
        itemStyle : {
        	normal: {
        		color: function(params) {
                    var colorList = [
                       '#FF3333','#00c0ef'
                       ];
                    return colorList[params.dataIndex%2]
                },
        	}
        },
        series: [{
            name: '所有机会数量',
            type: 'bar',
            barWidth : 50,
            data: [],
            itemStyle: {  
                normal: {  
                    label: {  
                        show: true,//是否展示  
                        position: 'top',
                        textStyle: {  
                            fontWeight:'bolder',  
                            fontSize : '12',  
                            fontFamily : '微软雅黑',  
                        }  
                    }  
                }  
            },
        },{
            name: '成功机会数量',
            type: 'bar',
            barWidth : 50,
            data: [],
            itemStyle: {  
                normal: {  
                    label: {  
                        show: true,//是否展示  
                        position: 'top',
                        textStyle: {  
                            fontWeight:'bolder',  
                            fontSize : '12',  
                            fontFamily : '微软雅黑',  
                        }  
                    }  
                }  
            },
        }]
    });
    myDeptChart.on('dblclick',function(params){
    	if(params.seriesName == '所有机会数量'){
    		openContractList(2,params.name);
    	}else{
    		openContractList(3,params.name);
    	}
    });
  //部门图表单击
    myDeptChart.on('click', function (params) {
    	queryData.beginDate = jQuery('#beginDate').val();
    	queryData.endDate = jQuery('#endDate').val();
    	//queryData.dateTimeType = jQuery('#dateTimeType').val();
    	queryData.deptName = params.name;
    	nowSelectDept = params.name;
    	isAll = params.seriesName;
    	var isSuccess = params.seriesName;
    	queryData.isSuccess = isSuccess;
    	var urlArea = window.rootPath + '/ui/chanceInfo/getAreaChart';//片区分析
    	jQuery.ajax({//片区分析
			url : encodeURI(urlArea),
			type : 'get',
			data : queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				// 指定图表的配置项和数据
				myAreaChart.setOption({
			        series: [{
		              name: '机会数',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
			        }]
			    });
			}
		});
    });
    //片区分析
    myAreaChart.setOption({
        title: {
            text: '片区分析'
        },
        tooltip: {},
        series : [{
              name: '机会数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
  //单击片区图表打开
    myAreaChart.on('click',function(params){
    	
    	if(isAll == "成功机会数量"){
    		isAll = '成功';
    	}else{
    		isAll = '';
    	}
    	openContractList(4,nowSelectDept,params.name);
    });
  //状态分析
    yonChart.setOption({
        title: {
            text: '状态分析'
        },
        tooltip: {},
        series : [{
              name: '机会数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
    yonChart.on('click',function(params){
    	openContractList(5,params.name);
    });
  //预计订货量分析
    countChart.setOption({
        title: {
            text: '预计订货量分析'
        },
        tooltip: {},
        series : [{
              name: '机会数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
    countChart.on('click',function(params){
    	openContractList(6,params.name);
    });

    //初始化加载一次非片区报表
	countDate();
    queryAllChart();
})

//初始化查询条件
function initQueryParam(){
	//$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
	//$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+((new Date().getDate()+"").length == 1 ? ("0"+new Date().getDate()) : new Date().getDate()));
	$('#endDate').val(getDay(1));
	$('#beginDate').val(new Date().getFullYear()+"-01-01");
}
function getDay(day){  
    var today = new Date();  
      
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;          

    today.setTime(targetday_milliseconds); //注意，这行是关键代码    
      
    var tYear = today.getFullYear();  
    var tMonth = today.getMonth();  
    var tDate = today.getDate();  
    tMonth = doHandleMonth(tMonth + 1);  
    tDate = doHandleMonth(tDate);  
    return tYear+"-"+tMonth+"-"+tDate;  
}  
function doHandleMonth(month){  
    var m = month;  
    if(month.toString().length == 1){  
       m = "0" + month;  
    }  
    return m;  
}  
//查询图标非片区 
function queryAllChart(){
	//initQueryParam();
	queryData.beginDate = jQuery('#beginDate').val();
	queryData.endDate = jQuery('#endDate').val();
	//queryData.dateTimeType = jQuery('#dateTimeType').val();
	if(jQuery('#beginDate').val() == ""){
		alert("查询开始时间不能为空!");
		return;
	}
	if(jQuery('#endDate').val() == ""){
		alert("查询结束时间不能为空!");
		return;
	}
	var urlThend = window.rootPath + '/ui/chanceInfo/getTrendChart';//趋势图
	//趋势图
	jQuery.ajax({
		url : encodeURI(urlThend),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myThenChart.setOption({
				xAxis : [
				            {
				                type : 'category',
				                boundaryGap : false,
				                data : data.Axis
				            }
				        ],
		        series : [
		            {
		                name:'所有机会数量',
		                type:'line',
		                data:data.seriesSumTotal,
		                markLine : {
		                    data : [
		                        {type : 'average', name: '平均值'}
		                    ]
		                }
		            },{
		                name:'成功机会数量',
		                type:'line',
		                data:data.CTseriesSumTotal,
		                markLine : {
		                    data : [
		                        {type : 'average', name: '平均值'}
		                    ]
		                }
		            }
		        ]
		        
		    });
		}
	});
	//部门分析
	var urlDept = window.rootPath + '/ui/chanceInfo/getChanceDeptChart';//部门分析
	jQuery.ajax({
		url : encodeURI(urlDept),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myDeptChart.setOption({
		        xAxis: {
		            data: data.Axis
		        },
		        series: [{
		            // 根据名字对应到相应的系列
		            name: '所有机会数量(个)',
		            barWidth : 50,
		            data: data.series
		        },
		        {
		            // 根据名字对应到相应的系列
		            name: '成功机会数量(个)',
		            barWidth : 50,
		            data: data.series2
		        }
		        ]
		    });
		}
	});
	//片区分析
    myAreaChart.setOption({
        title: {
            text: '片区分析'
        },
        tooltip: {},
        series : [{
              name: '机会数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
	//预计订货量分析
	var urlcountChart = window.rootPath + '/ui/chanceInfo/countChart';
	jQuery.ajax({
		url : encodeURI(urlcountChart),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			countChart.setOption({
		        series: [{
	              name: '机会数量',
	              type: 'pie',
	              radius: '55%',
	              data:data.data
		        }]
		    });
		}
	});
	
	//状态分析
	var urlYon = window.rootPath + '/ui/chanceInfo/yonChart';
	jQuery.ajax({
		url : encodeURI(urlYon),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			yonChart.setOption({
		        series: [{
	              name: '机会数量',
	              type: 'pie',
	              radius: '55%',
	              data:data.data
		        }]
		    });
		}
	});
	
	
	 
}

function countDate(){
	var mydate = new Date();
    var str = "" + mydate.getFullYear() + "-";
    str += (mydate.getMonth()+1) + "-";
    str += mydate.getDate();
    //本年信息
    queryData.beginDate = mydate.getFullYear()+"-01-01";
    queryData.endDate = str;
    var urlYearCount = window.rootPath + '/ui/chanceInfo/thisYearChanceCount';
    jQuery.ajax({
		url : encodeURI(urlYearCount),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			jQuery("#thisYearChanceCount").val(data.data);
		}
	});
    
    //本年成功信息
    var thisYearChanceSuccessCount = window.rootPath + '/ui/chanceInfo/thisYearChanceSuccessCount';
    jQuery.ajax({
		url : encodeURI(thisYearChanceSuccessCount),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			jQuery("#thisYearChanceSuccessCount").val(data.data);
		}
	});
    
  //本月信息
    var str = "" + mydate.getFullYear() + "-";
    str += (mydate.getMonth()+1)+"-01";
    queryData.beginDate = str;
    var thisMonthChanceCount = window.rootPath + '/ui/chanceInfo/thisYearChanceCount';
    jQuery.ajax({
		url : encodeURI(thisMonthChanceCount),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			jQuery("#thisMonthChanceCount").val(data.data);
		}
	});
    
}
	
//查询类型 1：部门 2：片区 3：行业 4：趋势 5：产品类型 6：合同类型
function openContractList(type,value,value1){
	var url = window.rootPath + "/ui/chanceTotal";
	if(type == 1){
		var beginDate = value.split("年")[0] + "-" + ((value.split("年")[1].split("月")[0] < 10) ? "0"+value.split("年")[1].split("月")[0] : value.split("年")[1].split("月")[0]) + "-01";
		var endDate = value.split("年")[0] + "-" + (((parseInt(value.split("年")[1].split("月")[0])+1) < 10) ? "0"+(parseInt(value.split("年")[1].split("月")[0])+1) : (parseInt(value.split("年")[1].split("月")[0])+1)) + "-01";
		if(value.split("年")[1].split("月")[0] == 12){//要进一个月
			endDate = parseInt(value.split("年")[0])+1 + "-01-01";
		}
		url += "?beginDate=" +beginDate + "&endDate="+endDate;
	}else if(type == 2){
		url += "?deptName=" +value+"&isSuccess=所有";
	}else if(type == 3){
		url += "?deptName=" +value+"&isSuccess=成功";
	}else if(type == 4){
		//alert(isAll);
		url += "?deptName=" +value + "&areaName="+value1+"&isSuccess="+isAll;
	}else if(type == 5){
		url += "?isSuccess=" +value;
	}else if(type == 6){
		url += "?chanceCount=" +value;
	}
	if(type != 1){
		url += "&beginDate=" + jQuery('#beginDate').val() + "&endDate=" + jQuery('#endDate').val();
	}
	
	window.open(url);
}